import re

# template:
# CREATE TABLE `t_dynamic_user_` (
#   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
#   `mid` int(11) NOT NULL DEFAULT '0' COMMENT '用户mid',
#   `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
#   `mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
#   PRIMARY KEY (`id`),
#   UNIQUE KEY `uk_mid` (`mid`),
#   KEY `ix_mtime` (`mtime`) USING BTREE
# ) ENGINE=InnoDB CHARSET=utf8 COMMENT='活跃用户表';

# 读取源文件模板内容
template_file = '/Users/timbloodzero/Java/dynamic_user_template.sql'
f1 = open(template_file, 'r+', encoding='UTF-8', errors='ignore')
text = f1.read()
print(text)

# 循环输出
target_file = '/Users/timbloodzero/Java/dynamic_user.sql'
f2 = open(target_file, 'w', encoding='UTF-8', errors='ignore')

for inx in range(100):
    table_inx = ""
    # 表
    if str(inx).__len__() == 1:
        table_inx = "0" + str(inx)
    else:
        table_inx = str(inx)
    relaced_text1 = re.sub('t_dynamic_user_', 't_dynamic_user_' + table_inx, text)
    relaced_text2 = re.sub('活跃用户表', '活跃用户表' + table_inx, relaced_text1)
    print(relaced_text2)
    if inx == 0:
        f2.write(relaced_text2)
    else:
        f2.write("\n" + relaced_text2)

f1.close()
f2.close()
